var app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    news: '',
    scrollTop: 0,
    message: '',
    contentData: [],
    contentData1:[{
      otherName: '徐静茹',
      otherImg: '/images/defaultImage.png',
      time: '2020-12-27 19:53',
      isRight: true,
      content: '哈哈哈哈哈',
      isImg: false,
      photoLink: '/images/illness2.png'
    },
    {
      otherName: '徐静茹2',
      otherImg: '/images/defaultImage.png',
      time: '2020-12-27 20:43',
      isRight: false,
      content: '张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉',
      isImg: false,
      photoLink: '/images/illness2.png'
    },
    {
      otherName: '张峻宇又在睡觉',
      otherImg: '/images/defaultImage.png',
      time: '2020-12-27 20:43',
      isRight: false,
      content: '张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉',
      isImg: true,
      photoLink: '/images/illness2.png'
    },
    {
      otherName: '张峻宇又在睡觉',
      otherImg: '/images/defaultImage.png',
      time: '2020-12-27 20:43',
      isRight: true,
      content: 'tui',
      isImg: true,
      photoLink: '/images/illness2.png'
    },
    ],
    selfImg: '/images/defaultImage.png',
    otherTel: '',
    otherName: 'hhh',
    doctorTel: '',
    isDoctor: '',
    kefu: '',
    oldLength: '',
    timer: ''
  },

  /**
   * 页面加载
   */
  onLoad: function (options) {
    this.setData({
      otherTel: JSON.parse(options.otherTel),
      otherName: JSON.parse(options.otherName),
      doctorTel: JSON.parse(options.doctorTel),
      kefu: JSON.parse(options.kefu),
      isDoctor: JSON.parse(options.isDoctor)
    });
    console.log(this.data.kefu)
    console.log(this.data.isDoctor)
    console.log(this.data.otherTel)
    console.log(this.data.doctorTel)
    var that = this
    wx.request({
      url:  getApp().globalData.url+'assistantGetSelfInfo',
      method: 'GET',
      data: {
        username: getApp().globalData.username,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        that.setData({
          selfImg: res.data.img,
        })
      },
      fail(res) {
        wx.showToast({
          title: '获取未读消息失败，请从顶部下拉刷新',
          icon: 'none',
          duration: 2000
        })
      }
    })
    this.getContent()
    wx.setNavigationBarTitle({ 
      title: that.data.otherName
    });
    setTimeout(function () {
      that.bottom()
    }, 500);
  },

  /**
   * 页面显示
   */
  onShow:function(e){
    this.setData({
      timer: setInterval(() => {
        var that = this
        if(that.data.kefu == true)
        {
          wx.request({
            url:  getApp().globalData.url + 'showpatientchatinfo',
            method: 'GET',
            data: {
              tel: getApp().globalData.username,
              doctorTel: null,
              otherTel: that.data.otherTel,
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'  // 默认值
            },
            success(res){
              if(res.data.length != that.data.oldLength)
              {
                that.getContent()
                that.bottom()
              }
            },
          })
        }
        else if(that.data.isDoctor == true)
        {
          wx.request({
            url:  getApp().globalData.url + 'showpatientchatinfo',
            method: 'GET',
            data: {
              tel: getApp().globalData.username,
              doctorTel: that.data.doctorTel,
              otherTel: null,
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'  // 默认值
            },
            success(res){
              if(res.data.length != that.data.oldLength)
              {
                that.getContent()
                that.bottom()
              }
            },
          })
        }
        else 
        {
          wx.request({
            url:  getApp().globalData.url + 'showpatientchatinfo',
            method: 'GET',
            data: {
              tel: getApp().globalData.username,
              doctorTel: that.data.doctorTel,
              otherTel: that.data.otherTel,
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'  // 默认值
            },
            success(res){
              if(res.data.length != that.data.oldLength)
              {
                that.getContent()
                that.bottom()
              }
            },
          })
        }
        console.log("轮播请求5秒触发一次");
      }, 5000)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //清除计时器  即清除setInter
    clearInterval(this.data.timer)
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    //清除计时器  即清除setIntertimer
    clearInterval(that.data.timer)
  },

  /**
   * 输入消息
   */
  bindInput: function (e) {
    this.setData({
      message: e.detail.value
    })
  },

  /**
   * 发送消息函数
   */
  send: function (e) {
    var that = this
    var message = this.data.message
    if( this.data.message != '' && this.data.message != undefined)
    {
      if(this.data.kefu == true)
      {
        console.log('您正在充当客服聊天')
        wx.request({
          url:  getApp().globalData.url+'assistantSendTextMessage',
          method: 'POST',
          data: {
            tel: getApp().globalData.username,
            doctorTel: null,
            otherTel: that.data.otherTel,
            content: that.data.message,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'  // 默认值
          },
          success(res){
            if (res.data == true) {
              that.bottom()
              that.getContent();
              that.setData({
                message: ''
              })
            } 
            else if(res.data==false){
              wx.showToast({
                title: '消息发送失败',
                icon: 'none',
              })
            }   
          },
          fail(res){
            wx.showToast({
              title: '网络连接中断，消息发送失败',
              icon: 'none',
            })
          }
        })
      }
      else if(this.data.isDoctor == true)
      {
        console.log('您正在与医生聊天')
        wx.request({
          url:  getApp().globalData.url+'assistantSendTextMessage',
          method: 'POST',
          data: {
            tel: getApp().globalData.username,
            doctorTel: that.data.doctorTel,
            otherTel: null,
            content: that.data.message,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'  // 默认值
          },
          success(res){
            if (res.data == true) {
              that.bottom()
              that.getContent();
              that.setData({
                message: ''
              })
            } 
            else if(res.data==false){
              wx.showToast({
                title: '消息发送失败',
                icon: 'none',
              })
            }   
          },
          fail(res){
            wx.showToast({
              title: '网络连接中断，消息发送失败',
              icon: 'none',
            })
          }
        })
      }
      else
      {
        console.log('您正在与普通患者聊天')
        wx.request({
          url:  getApp().globalData.url+'assistantSendTextMessage',
          method: 'POST',
          data: {
            tel: getApp().globalData.username,
            doctorTel: this.data.doctorTel,
            otherTel: this.data.otherTel,
            content: that.data.message,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'  // 默认值
          },
          success(res){
            if (res.data == true) {
              that.bottom()
              that.getContent();
              that.setData({
                message: ''
              })
            } 
            else if(res.data==false){
              wx.showToast({
                title: '消息发送失败',
                icon: 'none',
              })
            }   
          },
          fail(res){
            wx.showToast({
              title: '网络连接中断，消息发送失败',
              icon: 'none',
            })
          }
        })
      }
    }
    else
    {
      wx.showToast({
        title: '您发送的消息不能为空',
        icon: 'none',
      })
    }
  },

  /**
   * 获取聊天记录
   */ 
  getContent: function () {
    var that = this
    if(this.data.kefu == true)
    {
      wx.request({
        url:  getApp().globalData.url + 'showChatContent',
        method: 'GET',
        data: {
          tel: getApp().globalData.username,
          doctorTel: null,
          otherTel: that.data.otherTel,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'  // 默认值
        },
        success(res){
          console.log('kefu')
          
      console.log(res.data)
          if (res.data != "False" && res.data != "false" && res.data != false && res.data != "空") {
            that.setData({
              contentData: res.data,
              oldLength:res.data.length,
            })
            that.bottom()
          } 
          else if(res.data == "空"){
            that.setData({
              contentData: null,
              oldLength: 0,
            })
            that.bottom()
          }
          else{
            wx.showToast({
              title: '消息获取失败，请下拉刷新',
              icon: 'none',
            })
          }   
        },
        fail(res){
          wx.showToast({
            title: '网络连接中断，请下拉刷新',
            icon: 'none',
          })
        }
      })
    }
    else if(this.data.isDoctor == true)
    {
      console.log('doctor')
      wx.request({
        url:  getApp().globalData.url + 'showChatContent',
        method: 'GET',
        data: {
          tel: getApp().globalData.username,
          doctorTel: that.data.doctorTel,
          otherTel: null,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'  // 默认值
        },
        success(res){
          
      console.log(res.data)
          if (res.data != "False" && res.data != "false" && res.data != false && res.data != "空") {
            that.setData({
              contentData: res.data,
              oldLength:res.data.length,
            })
            that.bottom()
          } 
          else if(res.data == "空"){
            that.setData({
              contentData: null,
              oldLength: 0,
            })
            that.bottom()
          }
          else{
            wx.showToast({
              title: '消息获取失败，请下拉刷新',
              icon: 'none',
            })
          }   
        },
        fail(res){
          wx.showToast({
            title: '网络连接中断，请下拉刷新',
            icon: 'none',
          })
        }
      })
    }
    else 
    {
      console.log('putong')
      wx.request({
        url:  getApp().globalData.url + 'showChatContent',
        method: 'GET',
        data: {
          tel: getApp().globalData.username,
          doctorTel: that.data.doctorTel,
          otherTel: that.data.otherTel,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'  // 默认值
        },
        success(res){
          
      console.log(res.data)
          if (res.data != "False" && res.data != "false" && res.data != false && res.data != "空") {
            that.setData({
              contentData: res.data,
              oldLength:res.data.length,
            })
            that.bottom()
          } 
          else if(res.data == "空"){
            that.setData({
              contentData: null,
              oldLength: 0,
            })
            that.bottom()
          }
          else{
            wx.showToast({
              title: '消息获取失败，请下拉刷新',
              icon: 'none',
            })
          }   
        },
        fail(res){
          wx.showToast({
            title: '网络连接中断，请下拉刷新',
            icon: 'none',
          })
        }
      })
    }
  },

  /**
   * 获取hei的id节点然后屏幕焦点调转到这个节点 
   */ 
  bottom: function () {
    var query = wx.createSelectorQuery()
    query.select('#hei').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec(function (res) {
      wx.pageScrollTo({
        scrollTop: res[0].bottom  // #the-id节点的下边界坐标
      })
      res[1].scrollTop // 显示区域的竖直滚动位置
    })
  },

  /**
   * 选择图片并发送图片 
   */  
  sendImg: function () {
    var that = this;
    wx.chooseImage({
      success: function (res) 
      {
        var tempFilePaths = res.tempFilePaths[0]
        if(that.data.kefu == true)
        {
          console.log('您正在充当客服发送图片')
          wx.uploadFile({
            url: getApp().globalData.url + 'assistantSendPictureMessage',
            filePath: tempFilePaths,     
            name: 'content1', //文件对应的参数名字(key)              
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            formData: {
              tel: getApp().globalData.username,
              doctorTel: 'null',
              otherTel: that.data.otherTel,
            },
            success(res){
              if (res.data == true) {
                that.bottom()
                that.getContent()
              } 
              else if(res.data==false){
                wx.showToast({
                  title: '消息发送失败',
                  icon: 'none',
                })
              }   
            },
            fail(res){
              wx.showToast({
                title: '网络连接中断，消息发送失败',
                icon: 'none',
              })
            }
          })
        }
        else if(that.data.isDoctor == true)
        {
          console.log('您正在给医生发送图片')
          wx.uploadFile({
            url: getApp().globalData.url + 'assistantSendPictureMessage',
            filePath: tempFilePaths,     
            name: 'content1', //文件对应的参数名字(key)              
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            formData: {
              tel: getApp().globalData.username,
              doctorTel: that.data.doctorTel,
              otherTel: 'null',
            },
            success(res){
              console.log('test')
              console.log(res.data)
              if (res.data == true) {
                that.bottom()
                that.getContent()
              } 
              else if(res.data==false){
                wx.showToast({
                  title: '消息发送失败',
                  icon: 'none',
                })
              }   
            },
            fail(res){
              wx.showToast({
                title: '网络连接中断，消息发送失败',
                icon: 'none',
              })
            }
          })
          console.log('发给医生的图片发送完成')
        }
        else
        {
          console.log('您正在与普通患者发送图片')
          wx.uploadFile({
            url: getApp().globalData.url + 'assistantSendPictureMessage',
            filePath: tempFilePaths,     
            name: 'content1', //文件对应的参数名字(key)              
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            formData: {
              tel: getApp().globalData.username,
              doctorTel: that.data.doctorTel,
              otherTel: that.data.otherTel,
            },
            success(res){
              console.log('putonghuanzhe')
              console.log(res.data)
              if (res.data == true) {
                that.bottom()
                that.getContent()
              } 
              else if(res.data==false){
                wx.showToast({
                  title: '消息发送失败',
                  icon: 'none',
                })
              }   
            },
            fail(res){
              wx.showToast({
                title: '网络连接中断，消息发送失败',
                icon: 'none',
              })
            }
          })
        }
      }
    })
  }
})